package com.issac.messagequeue.mqserver.demo;

import com.issac.messagequeue.mqserver.coreclass.ExchangeType;
import com.issac.messagequeue.mqserver.mqclient.Channel;
import com.issac.messagequeue.mqserver.mqclient.Connection;
import com.issac.messagequeue.mqserver.mqclient.ConnectionFactory;

import java.io.IOException;

/**
 * 表示一个生产者
 * 通常是单独的服务器程序
 */
public class DemoProducer {
    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("启动生产者");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(9090);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        // 创建交换机和队列
        channel.exchangeDeclare("testDeclare", ExchangeType.DIRECT, true, false, null);
        channel.msgQueueDeclare("testMsgQueue", true, false, false, null);

        // 创建一个消息并发送
        byte[] messageBody = "hello".getBytes();
        boolean result = channel.basicPublish("testExchange", "testMsgQueue", null, messageBody);
        System.out.println("消息投递完成！ result=" + result);

        Thread.sleep(500);
        channel.close();
        connection.close();
    }
}
